package demiBoard;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import common.BaseServlet;
import common.gFunction;

/**
 * Servlet implementation class DemiBoardServlet
 */
@WebServlet("/boardPage")
public class DemiBoardServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DemiBoardServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
    /*
     * 프로시져
     */
    public void doAnyProc(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		// TODO Auto-generated method stub
    	initServlet( request,  response);
    	System.out.println("/boardPage");
    	System.out.println(reqFunc);
    	/*디폴트 일로오면 안된다*/
    	if(reqFunc.length()==0){
    		showBoard(request,response);
    	}
    	/*글쓰기로 가기*/
    	else if(reqFunc.equals("gotoPostBoard")){
    		gotoPostBoard(request,response);
    	}
    	/*글목록 보기*/
    	else if(reqFunc.equals("showBoard")){
    		showBoard(request,response);
    	}
    	/*글쓰기*/
    	else if(reqFunc.equals("postBoard")){
    		postBoard(request,response);
    	}
    	/*글읽기*/
    	else if(reqFunc.equals("boardReadPost")){
    		boardReadPost(request,response);
    	}
    	/*글어드민*/
    	else if(reqFunc.equals("goBoardAdmin")){
    		goBoardAdmin(request,response);
    	}
    	/*글추가*/
    	else if(reqFunc.equals("boardAdd")){
    		boardAdd(request,response);
    	}
    	/*글삭제*/
    	else if(reqFunc.equals("boardDelete")){
    		boardDelete(request,response);
    	}
    	/*글수정*/
    	else if(reqFunc.equals("boardModify")){
    		boardModify(request,response);
    	}
    	/*답글달기가기*/
    	else if(reqFunc.equals("goPostReply")){
    		goPostReply(request,response);
    	}
    	/*보드 목록 보여주기*/
    	else if(reqFunc.equals("showMenuBoardList")){
    		showMenuBoardList(request,response);
    	}
    	releaseServlet();
    			
   
    	
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doAnyProc(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doAnyProc(request,response);
	}
	/*
	 * 글쓰기
	 */
	protected void postBoard(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		System.out.println("글쓰기");
		//System.out.println(demiUser.getGrade());
		if(Integer.parseInt((String)session.getAttribute("userGrade"))<1){
			//비로그인쓰기불가
			System.out.println("실패?");
			
			request.setAttribute("massage","글쓰기는 일반회원 이상만 가능합니다");
			
			(request.getRequestDispatcher("/demiBoard/demiBoardList.jsp")).forward(request, response);
		}
		else
		{
			System.out.println("글쓰기 성공");
			/*파라미터 받기*/

			int cBoardId=Integer.parseInt(request.getParameter("reqBoardId"));
			int cMother=Integer.parseInt(gFunction.getNotNull(request.getParameter("reqPostId"),"0"));
			String cPostName=request.getParameter("postName");
			String cPostData=request.getParameter("postData");
			String cAuthor=(String)session.getAttribute("userId");
		
			
			//로그인 상태 글쓰기
		
			demiBoard.postBoard(cBoardId, cPostName, cPostData, cAuthor, cMother);
			
			request.setAttribute("message","글쓰기는완료");
			
			showBoard(request,response);
			}
	}
	/*
	 * 글리스트보여주기
	 */
	protected void showBoard(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		ArrayList boardList=null;
		System.out.println("글리스트보여주기");
		/*1.보드 넘버
		 *2.페이지
		 *3.보여줄 리스트 겟수
		 */
		int cBoardId=Integer.parseInt((String)request.getParameter("reqBoardId"));
		int PageNum=Integer.parseInt((String)request.getParameter("reqBoardPage"));
		System.out.println("cBoardId="+cBoardId);
		System.out.println("PageNum="+PageNum);
		
		/******게시판의 속성을 가져온다**/
		HashMap boardProperMap=demiBoard.getBoardProper(cBoardId);
		
		int listCount=Integer.parseInt((String)boardProperMap.get("cBoardPerPostNum"));//한페이지에 포스트겟수
		
		Integer numOfPage =demiBoard.getPageNumOfPage(cBoardId, listCount);//총페이지
		
		boardList=demiBoard.getList(cBoardId, PageNum, listCount);//게시판 리스트
		
		Integer numOfPost=demiBoard.getNumOfPost(cBoardId);
		
		request.setAttribute("boardList", boardList);
		request.setAttribute("boardProperMap", boardProperMap);
		request.setAttribute("numOfPage", numOfPage.toString());
		request.setAttribute("numOfPost", numOfPost.toString());
		
		
		
		(request.getRequestDispatcher("/demiBoard/demiBoardList.jsp")).forward(request, response);
	}
	/*
	 * 글쓰기로 이동
	 */
	protected void gotoPostBoard(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		System.out.println("글쓰기이동");
		
		(request.getRequestDispatcher("/demiBoard/demiBoardPost.jsp")).forward(request, response);
	}
	/*
	 * 글보기
	 */
	protected void boardReadPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		System.out.println("글보기");
		String cPostId=request.getParameter("reqPostId");
		//글정보
		HashMap tempMap =demiBoard.getPostData(Integer.parseInt(cPostId));
		request.setAttribute("postData", tempMap);
		(request.getRequestDispatcher("/demiBoard/demiBoardRead.jsp")).forward(request, response);
	}
	
	/*
	 * 글어드민로 이동
	 */
	protected void goBoardAdmin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		System.out.println("글어드민로");
		ArrayList boardList=demiBoard.getBoardList();
		request.setAttribute("boardAdminList", boardList);
		
		(request.getRequestDispatcher("/demiBoard/demiBoardAdmin.jsp")).forward(request, response);
	}
	/*
	 * 보드추가
	 */
	protected void boardAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		System.out.println("보드추가");
		String cBoardName=request.getParameter("cBoardName");
		int cBoardPostNumber=Integer.parseInt((String)request.getParameter("cBoardPostNumber"));
		int cBoardPageNumber=Integer.parseInt((String)request.getParameter("cBoardPageNumber"));
		
		demiBoard.addBoard(cBoardName,cBoardPostNumber,cBoardPageNumber);
		
		
		goBoardAdmin(request,response);
	}
	
	/*
	 * 보드삭제
	 */
	protected void boardDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		System.out.println("보드삭제");
		int cBoardId=Integer.parseInt((String)request.getParameter("cBoardId"));
		
		demiBoard.deleteBoard(cBoardId);
		goBoardAdmin(request,response);
	}
	/*
	 * 보드수정
	 */
	protected void boardModify(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		System.out.println("보드수정");
		int cBoardId=Integer.parseInt((String)request.getParameter("cBoardId"));
		String cBoardName=request.getParameter("cBoardName");
		int cBoardPostNumber=Integer.parseInt((String)request.getParameter("cBoardPostNumber"));
		int cBoardPageNumber=Integer.parseInt((String)request.getParameter("cBoardPageNumber"));
		
		demiBoard.modifyBoard(cBoardId, cBoardName,cBoardPostNumber,cBoardPageNumber);
		goBoardAdmin(request,response);
		
	}
	/*
	 * 답글달기이동
	 */
	protected void goPostReply(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		
		(request.getRequestDispatcher("/demiBoard/demiBoardPostReply.jsp")).forward(request, response);
		
	}
	/*
	 * 보드 목록 보여주기
	 */
	protected void showMenuBoardList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		ArrayList boardMenuList;
		System.out.println("보드 목록 보여주기");
		boardMenuList=demiBoard.getBoardList();
		request.setAttribute("boardMenuList", boardMenuList);
		(request.getRequestDispatcher("/demiBoard/demiBoardMenuList.jsp")).forward(request, response);
		
	}
	

	
	
	
	

}
